import java.rmi.RemoteException;

import common_classes.AuctionStates;
import common_classes.CustomerData;

import FlexTrade.WSAdaptor;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author hieuht
 * User interface when customer wants to place a bid to auction
 */
public class PlaceBidDlg extends javax.swing.JDialog {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/** Creates new form PlaceBidDlg */
    public PlaceBidDlg(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        btnOK = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        txtProduct = new javax.swing.JTextField();
        txtProduct.setEditable(false);
        txtProvider = new javax.swing.JTextField();
        txtProvider.setEditable(false);
        txtHighestPrice = new javax.swing.JTextField();
        txtHighestPrice.setEditable(false);
        txt2ndHighestPrice = new javax.swing.JTextField();
        txt2ndHighestPrice.setEditable(false);
        txtYourBid = new javax.swing.JTextField();
        lblProduct = new javax.swing.JLabel();
        lblProvider = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        lblTimeLeft = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtType = new javax.swing.JTextField();
        txtType.setEditable(false);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        btnOK.setText("OK");
        btnOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOKActionPerformed(evt);
            }
        });

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        lblProduct.setText("Product");

        lblProvider.setText("Provider");

        jLabel1.setText("Highest price");

        jLabel2.setText("2nd Highest Price");

        jLabel3.setText("Your Bid");

        lblTimeLeft.setText("Time Left:");

        jLabel4.setText("Auction Type");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(109, 109, 109)
                        .addComponent(btnOK, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(53, 53, 53)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblProduct)
                            .addComponent(lblProvider)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))))
                .addGap(67, 67, 67)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txtType, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addContainerGap(138, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(txtHighestPrice, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE)
                                .addComponent(txtYourBid, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE)
                                .addComponent(txt2ndHighestPrice, javax.swing.GroupLayout.DEFAULT_SIZE, 151, Short.MAX_VALUE))
                            .addGap(64, 64, 64))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(txtProduct, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txtProvider, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 179, Short.MAX_VALUE))
                            .addContainerGap()))))
            .addGroup(layout.createSequentialGroup()
                .addGap(180, 180, 180)
                .addComponent(lblTimeLeft)
                .addContainerGap(220, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(txtType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblProduct)
                    .addComponent(txtProduct, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblProvider)
                    .addComponent(txtProvider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtHighestPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txt2ndHighestPrice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtYourBid, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addComponent(lblTimeLeft)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnOK)
                    .addComponent(btnCancel))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>

    private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    	if(!checkDataValidity()) {
    		System.out.println("<checkValidity> Invalid data input. Bid price should be numeric.");
    		return;
    	}
    	int auctionType = this.auctionPrimitiveData.getAuctionType();
    	switch(auctionType){
    	case IAuction.VICKREY_AUCTION_TYPE:	//Vickrey auction
    		break;
    	case IAuction.ENGLISH_AUCTION_TYPE:	//English auction
    		break;
    	case IAuction.DUTCH_AUCTION_TYPE:	//Dutch auction
    		break;
    	default:	//invalid auction type
     	}
    	
    	float _bidPrice = Float.parseFloat(txtYourBid.getText());
    	common_classes.BidPriceData _bidPriceData = new common_classes.BidPriceData();
    	_bidPriceData.setCustomer(this.customerData);
    	_bidPriceData.setBidPrice(_bidPrice);
    	try {
			wsAdapter.receiveBidPriceFromCustomer(this.auctionPrimitiveData.getUniqueAuctionID(), _bidPriceData);
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	this.dispose();
    }

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    	this.dispose();
    }
    
    public boolean checkDataValidity(){
    	return true;
    }
    
    public void setCustomer(Customer _customer){
    	this.customer = _customer;
    }
    
    public void setWebService(WSAdaptor _adapter) {
    	this.wsAdapter = _adapter;
    }
    
    public void setCustomerData(CustomerData _customerData) {
    	this.customerData = _customerData;
    }
    
    public void setAuctionPrimitiveData(common_classes.AuctionPrimitiveData _auctionPrimitiveData){
    	float _highestPrice = -1;
    	float _2ndHighestPrice = -1;
    	this.auctionPrimitiveData = _auctionPrimitiveData;
    	txtProduct.setText(_auctionPrimitiveData.getProduct().getProductName());
    	txtProvider.setText(_auctionPrimitiveData.getProvider().getUniqueProviderID());
    	txtType.setText(Integer.toString(_auctionPrimitiveData.getAuctionType()));
    	try {
			AuctionStates currentAuctionState = wsAdapter.getAuctionState(_auctionPrimitiveData.getUniqueAuctionID());
	    	_highestPrice = currentAuctionState.getHighestBidPrice();
	    	_2ndHighestPrice = currentAuctionState.getSecondHighestBidPrice();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

    	txtHighestPrice.setText(Float.toString(_highestPrice));
    	txt2ndHighestPrice.setText(Float.toString(_2ndHighestPrice));
    }
    
    // Variables declaration - do not modify
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnOK;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel lblProduct;
    private javax.swing.JLabel lblProvider;
    private javax.swing.JLabel lblTimeLeft;
    private javax.swing.JTextField txt2ndHighestPrice;
    private javax.swing.JTextField txtHighestPrice;
    private javax.swing.JTextField txtProduct;
    private javax.swing.JTextField txtProvider;
    private javax.swing.JTextField txtType;
    private javax.swing.JTextField txtYourBid;
    // End of variables declaration
    
    public Customer customer;
    public common_classes.CustomerData customerData;
    public WSAdaptor wsAdapter;
    public common_classes.AuctionPrimitiveData auctionPrimitiveData;
}
